﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using IntfClass;
using System.Data.SqlClient;
using InvokeWebService;
using SunLightIntfService;
namespace IntfSetting.FrmIntf
{
    public partial class FrmIntfMain : Form
    {
        public FrmIntfMain()
        {
            InitializeComponent();
        }

        private DataTable dtGridView = new DataTable(); 

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            FrmIntfSetting frmIntfSetting = new FrmIntfSetting(string.Empty);
            frmIntfSetting.ShowDialog();
            this.SearchGridView();
        }

        private void FrmIntfMain_Load(object sender, EventArgs e)
        {
            SearchGridView();


        }

        private void SearchGridView()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendLine("Select t.ID,t.Code,t.Name");
            strSql.AppendLine(",case t.Status when 0 then '暂停'when 1 then '启动' else ''end as Status");
            strSql.AppendLine(",t.Memo from TIntf t");

            ClassDB classDB = new ClassDB();
            dtGridView = classDB.ExecuteDataTable(strSql.ToString());

            this.DgvInterface.DataSource = dtGridView;
            DgvInterface.Columns[0].Visible = false;
            DgvInterface.Columns[1].Visible = true;
            DgvInterface.Columns[2].Visible = true;
            DgvInterface.Columns[3].Visible = true;
            DgvInterface.Columns[4].Visible = true;

            DgvInterface.Columns[1].ReadOnly = true;
            DgvInterface.Columns[2].ReadOnly = true;
            DgvInterface.Columns[3].ReadOnly = true;
            DgvInterface.Columns[4].ReadOnly = true;

            DgvInterface.Columns[1].HeaderText = "编号";
            DgvInterface.Columns[2].HeaderText = "名称";
            DgvInterface.Columns[3].HeaderText = "状态";
            DgvInterface.Columns[4].HeaderText = "备注";


        }

        private void btnUpt_Click(object sender, EventArgs e)
        {
            if (DgvInterface.SelectedRows[0].Cells["Code"].Value == null)
            {
                return;
            }
            FrmIntfSetting frmIntfSetting = new FrmIntfSetting(DgvInterface.SelectedRows[0].Cells["Code"].Value.ToString());
            frmIntfSetting.ShowDialog();
            this.SearchGridView();

        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (DgvInterface.SelectedRows[0].Cells["Code"].Value == null)
            {
                return;
            }

            StringBuilder strSql = new StringBuilder();
            strSql.AppendLine("Select t.* from TRule t");
            strSql.AppendLine("Where t.ParentCode = '" + DgvInterface.SelectedRows[0].Cells["Code"].Value.ToString() + "'");

            DataTable dtData = new DataTable();
            ClassDB classDB = new ClassDB();
            dtData = classDB.ExecuteDataTable(strSql.ToString());

            SqlConnection conn = classDB.CreateConnection();
            conn.Open();
            SqlTransaction sqlTransaction = conn.BeginTransaction();

            try
            {
                strSql = new StringBuilder();
                for (int i = 0; i < dtData.Rows.Count; i++)
                {
                    strSql.AppendLine("Delete from TRulelist Where ParentID = " + dtData.Rows[i]["ID"].ToString());
                    strSql.AppendLine(";");
                }
                //strSql.AppendLine("Delete from TRulelist Where ParentID = " + dtData.Rows[1]["ID"].ToString());
                //strSql.AppendLine(";");

                classDB.ExecuteNone(strSql.ToString(), conn, sqlTransaction, null);

                strSql = new StringBuilder();
                strSql.AppendLine("Delete from TIntfURL Where ParentCode = '" + DgvInterface.SelectedRows[0].Cells["Code"].Value.ToString() + "'");
                strSql.AppendLine(";");
                strSql.AppendLine("Delete from TRule Where ParentCode = '" + DgvInterface.SelectedRows[0].Cells["Code"].Value.ToString() + "'");
                strSql.AppendLine(";");
                classDB.ExecuteNone(strSql.ToString(), conn, sqlTransaction, null);

                strSql = new StringBuilder();
                strSql.AppendLine("Delete from TIntf Where ID = '" + DgvInterface.SelectedRows[0].Cells["ID"].Value.ToString() + "'");
                classDB.ExecuteNone(strSql.ToString(), conn, sqlTransaction, null);

                sqlTransaction.Commit();
            }
            catch (Exception ex)
            {

                sqlTransaction.Rollback();

                MessageBox.Show(ex.Message);

                return;
            }

            this.SearchGridView();




        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (DgvInterface.SelectedRows[0].Cells["Code"].Value == null)
            {
                return;
            }

            if (DgvInterface.SelectedRows[0].Cells["Status"].Value.ToString().Equals("启动"))
            {
                return;
            }

            StringBuilder strSql = new StringBuilder();
            strSql.AppendLine("Select t.* from TRule t");
            strSql.AppendLine("Where t.ParentCode = '" + DgvInterface.SelectedRows[0].Cells["Code"].Value.ToString() + "'");

            DataTable dtData = new DataTable();
            ClassDB classDB = new ClassDB();
            dtData = classDB.ExecuteDataTable(strSql.ToString());

            string[] strRuleID;

            if (dtData.Rows.Count > 0)
            {
                strRuleID = new string[dtData.Rows.Count];

                for (int i = 0; i < dtData.Rows.Count; i++)
                {
                    strRuleID[i] = dtData.Rows[i]["ID"].ToString();
                }

                SqlParameter sqlParameter2 = new SqlParameter("ID", SqlDbType.Int);

                strSql.AppendLine("Update TIntf set Status = 1 Where ID = @ID");

                sqlParameter2.Value = Convert.ToInt64(DgvInterface.SelectedRows[0].Cells["ID"].Value.ToString());

                classDB.ExecuteNone(strSql.ToString(), new SqlParameter[] { sqlParameter2 });

                //重新计算下次执行时间
                ClassIntf classIntf = new ClassIntf();
                string message = classIntf.UpdateExecutionTime(strRuleID, new DateTime());
                //service1.CacuNextExecutionTime(strRuleID, Convert.ToDateTime(dtData.Rows[0]["DBTime"].ToString()));

            }
            //DataRow[] drUp = dtData.Select("blnUp = 1");
            //DataRow[] drDown = dtData.Select("blnUp = 0");

            //strSql = new StringBuilder();
            //strSql.AppendLine("select * from TRuleList t where t.ParentID = " + drUp[0]["ID"].ToString());
            //DataTable dtUp = new DataTable();
            //DataTable dtDown = new DataTable();
            //dtUp = ClassDB.ExecuteDataTable(strSql.ToString());

            //DateTime dateCacu = DateTime.Now;
            //intfsetting setting = new intfsetting();
            //SqlConnection conn = ClassDB.CreateConnection();
            //conn.Open();
            //SqlTransaction sqlTransaction = conn.BeginTransaction();
            //try
            //{
            //    strSql = new StringBuilder();

            //    if (drUp[0]["kind"].ToString().Equals("1"))
            //    {
            //        if (drUp[0]["blnTiming"].ToString().Equals("1"))
            //        {
            //            dateCacu = dateCacu.AddMinutes(Convert.ToDouble(drUp[0]["Frequency"]));

            //        }
            //        else
            //        {
            //            dateCacu = setting.CalculateExecutionTime(drUp[0], dtUp);
            //        }

            //    }
            //    else
            //    {
            //        dateCacu = setting.CalculateExecutionTime(drUp[0], dtUp);
            //    }

            //    strSql.AppendLine("Update TRule set ExecutionTime = @ExecutionTime Where ID = @ID");

            //    SqlParameter sqlParameter1 = new SqlParameter("ExecutionTime", SqlDbType.DateTime);
            //    SqlParameter sqlParameter2 = new SqlParameter("ID", SqlDbType.Int);
                
            //    sqlParameter1.Value = dateCacu;
            //    sqlParameter2.Value = drUp[0]["ID"];
            //    ClassDB.ExecuteNone(strSql.ToString(), conn, sqlTransaction, new SqlParameter[] { sqlParameter1, sqlParameter2 });

            //    if (drDown.Length > 0)
            //    {
            //        strSql = new StringBuilder();
            //        strSql.AppendLine("select * from TRuleList t where t.ParentID = " + drDown[0]["ID"].ToString());

            //        dtDown = ClassDB.ExecuteDataTable(strSql.ToString());
            //        if (drDown[0]["kind"].ToString().Equals("1"))
            //        {
            //            if (drDown[0]["blnTiming"].ToString().Equals("1"))
            //            {
            //                dateCacu = dateCacu.AddMinutes(Convert.ToDouble(drDown[0]["Frequency"]));

            //            }
            //            else
            //            {
            //                dateCacu = setting.CalculateExecutionTime(drDown[0], dtUp);
            //            }

            //        }
            //        else
            //        {
            //            dateCacu = setting.CalculateExecutionTime(drDown[0], dtUp);
            //        }

            //        sqlParameter1.Value = dateCacu;
            //        sqlParameter2.Value = drDown[0]["ID"];
            //        ClassDB.ExecuteNone(strSql.ToString(), conn, sqlTransaction, new SqlParameter[] { sqlParameter1, sqlParameter2 });
            //    }

            //    strSql = new StringBuilder();
            //    strSql.AppendLine("Update TIntf set Status = 1 Where ID = @ID");
            //    sqlParameter2.Value = Convert.ToInt64(dataGridView1.SelectedRows[0].Cells["Code"].Value.ToString());
            //    ClassDB.ExecuteNone(strSql.ToString(), conn, sqlTransaction, new SqlParameter[] { sqlParameter2 });

            //    sqlTransaction.Commit();
            //}
            //catch (Exception ex)
            //{

            //    sqlTransaction.Rollback();
            //    MessageBox.Show(ex.Message);
            //}
            this.SearchGridView();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (DgvInterface.SelectedRows[0].Cells["Code"].Value == null)
            {
                return;
            }

            if (DgvInterface.SelectedRows[0].Cells["Status"].Value.ToString().Equals("暂停"))
            {
                return;
            }
            ClassDB classDB = new ClassDB();

            SqlConnection conn = classDB.CreateConnection();
            conn.Open();
            SqlTransaction sqlTransaction = conn.BeginTransaction();
            try
            {
                StringBuilder strSql = new StringBuilder();

                SqlParameter sqlParameter2 = new SqlParameter("ID", SqlDbType.Int);

                strSql.AppendLine("Update TIntf set Status = 0 Where ID = @ID");

                sqlParameter2.Value = Convert.ToInt64(DgvInterface.SelectedRows[0].Cells["ID"].Value.ToString());

                classDB.ExecuteNone(strSql.ToString(), conn, sqlTransaction, new SqlParameter[] { sqlParameter2 });
                sqlTransaction.Commit();

            }
            catch (Exception ex)
            {

                sqlTransaction.Rollback();
                MessageBox.Show(ex.Message);
            }
            this.SearchGridView();
        }
    }
}
